package fxTrader.algorithm;
import java.math.BigDecimal;
import java.util.ArrayList;

import updater.currency.data.Currency;

public class TrendFinder {
	private double[] prices;

	public boolean MakeDecision_shortTrade(ArrayList<Currency> currency) {

		prices = new double[currency.size()];
		double average = 0;

		for (int i = 0; i < currency.size(); i++) {
			prices[i] = ((Currency) currency.get(i)).getPrice();
		}

		average = CalculateAverage_20Days(prices);

		// when program return true , user is suggested to buy  the current
		// when program return false, user is suggested to sell the current
		return (average > prices[prices.length - 1]) ? true : false;
	}
	
	public boolean MakeDecision_longTrade(ArrayList<Currency> currency) {

		prices = new double[currency.size()];
		double average = 0;

		for (int i = 0; i < currency.size(); i++) {
			prices[i] = ((Currency) currency.get(i)).getPrice();
		}

		average = CalculateAverage_50Days(prices);

		// when program return true , user is suggested to buy  the current
		// when program return false, user is suggested to sell the current
		return (average > prices[prices.length - 1]) ? true : false;
	}

	private double CalculateAverage_20Days(double[] prices) { // calculate 20 days currency average
		double average = 0;
		int i;
		i = (prices.length - 2 >= 0) ? prices.length - 2 : 0;
		boolean isValid = true;
		while (isValid) {
			average += prices[i];
			i--;
			if (i < 0 || i < prices.length - 21) {
				isValid = false;
			}
		}
		average /= 20;
		return average;
	}
	//------------------------------------------------------------------------------------------------------------------ Thomas update.
	
	private double CalculateAverage_10Days(double[] prices) { // calculate 10 days currency average
		double average = 0;
		int i, count;
		i = (prices.length - 2 >= 0) ? prices.length - 2 : 0;

		boolean isValid = true;
		while (isValid) {
			average += prices[i];
			i--;
			if (i < 0 || i < prices.length - 11) {
				isValid = false;
			}
		}
		average /= 10;
		return average;
	}
	
	private double CalculateAverage_50Days(double[] prices) { // calculate 50 days currency average
		double average = 0;
		int i;
		i = (prices.length - 2 >= 0) ? prices.length - 2 : 0;
		boolean isValid = true;
		while (isValid) {
			average += prices[i];
			i--;
			if (i < 0 || i < prices.length - 51) {
				isValid = false;
			}
		}
		average /= 50;
		return average;
	}
	
	public double[] getResultList(ArrayList<Currency> currency) { // return average list for useful information
		prices = new double[currency.size()];

		for (int i = 0; i < currency.size(); i++) {
			prices[i] = ((Currency) currency.get(i)).getPrice();
		}
		
		double[] aveList = {GetCurrentRate(prices),CalculateAverage_10Days(prices),
							CalculateAverage_20Days(prices),CalculateAverage_50Days(prices),
							FindMax_5Days(prices),FindMax_1Month(prices),
							FindMin_5Days(prices),FindMin_1Month(prices)};
		
		double[] tempList = new double[3];
		
		for (int i = 0; i < aveList.length; i++) {
			tempList = aveList;
			BigDecimal temp = new BigDecimal(tempList[i]);
			aveList[i] = temp.setScale(4, BigDecimal.ROUND_HALF_UP).doubleValue();
		}
		return aveList;
	}
	
	private double FindMax_5Days(double[] prices) { // find MAX in 50 days 
		double maximum = 0;// start with the last 2 value
		int i;
		i = (prices.length - 2 >= 0) ? prices.length - 2 : 0;
		boolean isValid = true;
		maximum = prices[i];
		while (isValid) {			
			if (prices[i] > maximum) {
	            maximum = prices[i];   // new maximum
	        }
			i--;
			if (i < 0 || i < prices.length - 6) {
				isValid = false;
			}
		}
		return maximum;
	}
	
	private double FindMax_1Month(double[] prices) { // find MAX in 50 days 
		double maximum = 0;// start with the last 2 value
		int i;
		i = (prices.length - 2 >= 0) ? prices.length - 2 : 0;
		boolean isValid = true;
		maximum = prices[i];
		while (isValid) {			
			if (prices[i] > maximum) {
	            maximum = prices[i];   // new maximum
	        }
			i--;
			if (i < 0 || i < prices.length - 31) {
				isValid = false;
			}
		}
		return maximum;
	}
	
	private double FindMin_5Days(double[] prices) { // find MIN in 5 days 
		double minimum = 0;// start with the last 2 value
		int i;
		i = (prices.length - 2 >= 0) ? prices.length - 2 : 0;
		boolean isValid = true;
		minimum = prices[i];
		while (isValid) {			
			if (prices[i] < minimum) {
				minimum = prices[i];   // new maximum
	        }
			i--;
			if (i < 0 || i < prices.length - 6) {
				isValid = false;
			}
		}
		return minimum;
	}
	
	private double FindMin_1Month(double[] prices) { // find MIN in 1 month
		double minimum = 0;// start with the last 2 value
		int i;
		i = (prices.length - 2 >= 0) ? prices.length - 2 : 0;
		boolean isValid = true;
		minimum = prices[i];
		while (isValid) {			
			if (prices[i] < minimum) {
				minimum = prices[i];   // new maximum
	        }
			i--;
			if (i < 0 || i < prices.length - 31) {
				isValid = false;
			}
		}
		return minimum;
	}
	
	private double GetCurrentRate(double[] prices) { // return current rate
		return prices[prices.length - 1];
	}
}